package com.example.controller;

import com.example.common.log.LogAnnotation;
import com.example.entity.SysUser;
import com.example.service.LoginService;
import com.example.resp.ErrorCode;
import com.example.resp.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@Slf4j
public class LoginController {
    @Resource
    private LoginService loginService;

    @LogAnnotation(module = "登录模块",operation = "用户登录")
    @GetMapping("/login")
    public Result login(@RequestParam String username, @RequestParam String password, HttpSession session) {

        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return Result.fail(ErrorCode.ACCOUNT_PWD_ERROR.getCode(),ErrorCode.ACCOUNT_PWD_ERROR.getMsg());
        }
        log.info("用户输入username:"+username);
        log.info("用户输入password:"+password);
//        System.out.println(1/0);

        SysUser user = loginService.login(username, password);
        if(user==null){
            return Result.fail(ErrorCode.ACCOUNT_PWD_ERROR.getCode(),ErrorCode.ACCOUNT_PWD_ERROR.getMsg());
        }

        log.info("登录时的session：" + session);
        session.setAttribute("USER_SESSION", user);
        log.info("注入session成功？：" + session.getAttribute("USER_SESSION"));

        return Result.success();
    }

}
